package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.domain.po.Clazz;
import com.itheima.domain.vo.PageResult;
import com.itheima.domain.vo.Result;
import com.itheima.service.ClazzService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@Tag(name = "班级管理", description = "班级管理相关接口")
@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    @Operation(summary = "分页查询班级", description = "根据条件分页查询班级列表")
    @GetMapping
    public Result list(@Parameter(description = "班级名称") String name,
            @Parameter(description = "开始日期") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @Parameter(description = "结束日期") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "页面大小") @RequestParam(defaultValue = "10") int pageSize) {
        log.info("查询班级列表");
        PageResult pageResult = clazzService.list(name, begin, end, page, pageSize);
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        System.out.println("--------------------------------------");
        return Result.success(pageResult);
    }

    @Operation(summary = "新增班级", description = "新增班级信息")
    @Log
    @PostMapping
    public Result save(@Parameter(description = "班级信息") @RequestBody Clazz clazz) {
        log.info("新增班级：{}", clazz);
        clazzService.saveClazz(clazz);
        return Result.success();
    }

    @Operation(summary = "根据ID查询班级", description = "根据班级ID查询班级详细信息")
    @GetMapping("/{id}")
    public Result getById(@Parameter(description = "班级ID") @PathVariable Integer id) {
        log.info("根据id查询班级：{}", id);
        return Result.success(clazzService.getById(id));
    }

    @Operation(summary = "修改班级", description = "更新班级信息")
    @Log
    @PutMapping
    public Result update(@Parameter(description = "班级信息") @RequestBody Clazz clazz) {
        log.info("修改班级：{}", clazz);
        clazzService.updateClazzById(clazz);
        return Result.success();
    }

    @Operation(summary = "删除班级", description = "根据班级ID删除班级")
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@Parameter(description = "班级ID") @PathVariable Integer id) {
        log.info("删除班级：{}", id);
        clazzService.removeById(id);
        return Result.success();
    }

    @Operation(summary = "查询全部班级", description = "获取所有班级列表")
    @GetMapping("/list")
    public Result list() {
        log.info("查询全部班级");
        return Result.success(clazzService.list());
    }
}
